package com.sdhs.paas.redisservice.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdhs.paas.redisservice.dto.RedisStoreDTO;
import com.sdhs.paas.redisservice.entity.RedisBean;

@RestController
@RequestMapping(value = "/redis")
public class RedisTestController {
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 存储key-value
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/setStrKeyValue")
	public String setStrKeyValue(@RequestBody RedisBean redisBean){
		redisTemplate.opsForValue().set(redisBean.getKey(), redisBean.getValue());
		return "success";
	}
	
	/**
	 * 通过可以获取value
	 * @param key：redis存储的key
	 * */
	@GetMapping("/getStrKeyValue")
	public Object getStrKeyValue(@RequestParam(value = "key", required = true) String key){
		String value = String.valueOf(redisTemplate.opsForValue().get(key));
		return value;
	}
	
	/**
	 * 存储key-value，并设置超时时间
	 * @param key: redis的key
	 * 		  value: redis的value
	 * 		  expireSeconds： redis的超时时间
	 * */
	@PostMapping("/setStrKeyValueTimeout")
	public String setStrKeyValueTimeout(@RequestBody RedisBean redisBean){
		redisTemplate.opsForValue().set(redisBean.getKey(), redisBean.getValue(), redisBean.getExpireSeconds() ,TimeUnit.SECONDS);
		return "success";
	}
	
	/**
	 * 存储key-value，并设置超时时间
	 * @param key: redis的key
	 * 		  value: redis的value
	 * 	      offset: value的偏移量
	 * */
	@PostMapping("/setStrKeyValueOffset")
	public String setStrKeyValueOffset(@RequestBody RedisBean redisBean){
		redisTemplate.opsForValue().set(redisBean.getKey(), redisBean.getValue(), redisBean.getOffset());
		return "success";
	}
	
	
	/**
	 * 存储key-value 判断key-value值是否存在
	 * @param key: redis的key
	 * 		  value: redis的value
	 * */
	@PostMapping("/setIfAbsent")
	public String setIfAbsent(@RequestBody RedisBean redisBean){
		Boolean flag = redisTemplate.opsForValue().setIfAbsent(redisBean.getKey(), redisBean.getValue());
		return String.valueOf(flag);
	}
	
	/**
	 * 通过可以获取value
	 * @param key：redis存储的key
	 * */
	@PostMapping("/multiSet")
	public Object multiSet(@RequestBody RedisStoreDTO redisStoreDTO){
		List<RedisBean> redisBeanList = redisStoreDTO.getRedisBeanList();
		Map<String,String> maps = new HashMap<String, String>();

		redisBeanList.stream().forEach(item-> 
			{
				maps.put( item.getKey(), String.valueOf(item.getValue()));
			});
		redisTemplate.opsForValue().multiSet(maps);
		return "success";
	}
	
	/**
	 * 获取多个key的值
	 * @param List<Key>
	 * */
	@PostMapping("/getMultiSet")
	public Object getMultiSet(@RequestBody RedisStoreDTO redisStoreDTO){
		List<RedisBean> redisBeanList = redisStoreDTO.getRedisBeanList();
		List<String> keys = new ArrayList<String>();
		redisBeanList.stream().forEach(item-> 
		{
			keys.add(item.getKey());
		});
		List<Object> value = redisTemplate.opsForValue().multiGet(keys);
		return value;
	}
	
	/**
	 * 通过可以获取value
	 * @param List<Key>
	 * */
	@PostMapping("/multiSetIfAbsent")
	public String multiSetIfAbsent(@RequestBody RedisStoreDTO redisStoreDTO){
		List<RedisBean> redisBeanList = redisStoreDTO.getRedisBeanList();
		Map<String,String> maps = new HashMap<String, String>();
		redisBeanList.stream().forEach(item-> 
		{
			maps.put(item.getKey(),String.valueOf(item.getValue()));
		});
		Boolean flag = redisTemplate.opsForValue().multiSetIfAbsent(maps);
		return String.valueOf(flag);
	}
	
	/**
	 * 存储key-value,  将新的value覆盖原有值，并返回旧值
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/getAndSet")
	public Object getAndSet(@RequestBody RedisBean redisBean){
		Object obj = redisTemplate.opsForValue().getAndSet(redisBean.getKey(), redisBean.getValue());
		return obj;
	}
	
	/**
	 * 存储key-value,  存储整数,value是整数，递增的。
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/increment")
	public Object increment(@RequestBody RedisBean redisBean){
		Object obj = redisTemplate.opsForValue().increment(redisBean.getKey(), Long.valueOf(String.valueOf(redisBean.getValue())));
		return obj;
	}
	
	
	/**
	 * 存储key-value,  追加字符串。
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/append")
	public Object append(@RequestBody RedisBean redisBean){
		redisTemplate.opsForValue().append(redisBean.getKey(), String.valueOf(redisBean.getValue()));
		return "success";
	}
	
	/**
	 * 通过可以获取value
	 * @param key：redis存储的key
	 * */
	@GetMapping("/getSubStringValue")
	public Object getSubStringValue(@RequestParam(value = "key", required = true) String key, 
				@RequestParam(value = "start", required = true) Long start , 
				@RequestParam(value = "end", required = false) Long end){
		String value = String.valueOf(redisTemplate.opsForValue().get(key,start,end));
		return value;
	}
	
	
	/**
	 * 通过可以获取value的长度
	 * @param key：redis存储的key
	 * */
	@GetMapping("/getValueSize")
	public Object getValueSize(@RequestParam(value = "key", required = true) String key){
		String value = String.valueOf(redisTemplate.opsForValue().size(key));
		return value;
	}
	
	/**
	 * 存储key-value, 将一个集合存储到value里。
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/leftPushAll")
	public Object leftPushAll(@RequestBody RedisStoreDTO redisStoreDTO){
		List<RedisBean> redisBeanList = redisStoreDTO.getRedisBeanList();
		List<Object> list = new ArrayList<Object>();
		redisBeanList.stream().forEach(item -> {
			list.add(item.getValue());
		});
		redisTemplate.opsForList().leftPushAll(redisStoreDTO.getKey(), list);
		return "success";
	}
	
	/**
	 * 通过可以获取list集合
	 * @param key：redis存储的key
	 * */
	@GetMapping("/range")
	public Object range(@RequestParam(value = "key", required = true) String key){
		List<Object> list= redisTemplate.opsForList().range(key, 0, -1);
		return list;
	}
	
	
	/**
	 * 存储key-value, 将一个元素添加到集合的头部
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/leftPush")
	public Object leftPush(@RequestBody RedisBean redisBean){
		Long num = redisTemplate.opsForList().leftPush(redisBean.getKey(), redisBean.getValue());
		return num;
	}
	
	/**
	 * 存储key-value, 只有集合存在才能将元素插入集合里
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/leftPushIfPresent")
	public Object leftPushIfPresent(@RequestBody RedisBean redisBean){
		Long num = redisTemplate.opsForList().leftPushIfPresent(redisBean.getKey(), redisBean.getValue());
		return num;
	}
	
	/**
	 * 存储key-value, 将一个元素添加到集合的尾部
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/rightPush")
	public Object rightPush(@RequestBody RedisBean redisBean){
		Long num = redisTemplate.opsForList().rightPush(redisBean.getKey(), redisBean.getValue());
		return num;
	}
	
	
	/**
	 * 存储key-value, 将一个元素添加到集合的尾部
	 * @param key: redis的key
	 * 		  value: 存储的值
	 * 		 
	 * */
	@PostMapping("/rightPushIfPresent")
	public Object rightPushIfPresent(@RequestBody RedisBean redisBean){
		Long num = redisTemplate.opsForList().rightPushIfPresent(redisBean.getKey(), redisBean.getValue());
		return num;
	}
	
	
	/**
	 * 通过可以获取list集合的长度
	 * @param key：redis存储的key
	 * */
	@GetMapping("/getListsize")
	public Object getListsize(@RequestParam(value = "key", required = true) String key){
		Long size = redisTemplate.opsForList().size(key);
		return size;
	}
	
	
}
